package server

import (
	"context"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/timehelper"
	"longmen/server/pkg/db/mysql"
	"longmen/server/pkg/db/mysql/db_sports"
	"longmen/server/pkg/db/mysql/models/models_main"
	"longmen/server/pkg/db/redis/cache"
	"longmen/server/pkg/protobuf"
)

// 直播页菜单
func (s *Server) Menus(ctx context.Context, req *protobuf.Empty) (*protobuf.MenusResp, error) {
	begin, _ := timehelper.YesterdayAndToday()
	matchs, err := db_sports.GetTempMatches(map[string]interface{}{"match_time": map[string]interface{}{">=": begin}, "match_status": 1}, "", 1, 3)
	if err != nil {
		global.Log.Errorf("GetMenus matchs err:%v", err)
	}
	lives := make([]*protobuf.LiveInfo, 0)
	livesRedis, _ := cache.GetInstance().QueryLiveByCatAndStatus(0, 0, 1, 1, 3, true)
	//lives, err := repository.GetLivePbParam(map[string]interface{}{"status": 1}, "hot desc", 1, 3)
	if err != nil {
		global.Log.Errorf("GetMenus lives err:%v", err)
	}

	summaryCategory, _ := s.Category()
	for _, live := range livesRedis {
		lives = append(lives, &protobuf.LiveInfo{
			Title:        live.Title,
			Thumb:        live.Thumb,
			PullUrl:      live.PullUrl,
			Categoryid:   uint32(live.Categoryid),
			AnchorAvatar: live.AnchorAvatar,
			AnchorName:   live.AnchorName,
			Category:     uint32(live.Category),
			Hot:          uint32(live.Hot),
			Anchorid:     uint32(live.Anchorid),
			Matchid:      uint32(live.Matchid),
			Tag:          live.Tag,
			TagTitle:     "",
			TagColor:     "",
			TagPoint:     0,
			TagType:      0,
		})
	}
	if len(lives) > 0 {
		var tagIDs []int64
		for _, l := range lives {
			tagIDs = append(tagIDs, l.Tag)
		}

		queryTags, err2 := mysql.GetConfigTagByIds2(tagIDs)
		if err2 != nil {
			goto OUTPUT
		}

		for _, l := range lives {
			var tagInfo *models_main.ConfigTag

			for _, tag := range queryTags {
				if int64(tag.Id) == l.Tag {
					tagInfo = tag
					break
				}
			}

			if tagInfo != nil {
				l.TagTitle = tagInfo.Title
				l.TagColor = tagInfo.Color
				l.TagPoint = int32(tagInfo.Point)
				l.TagType = int32(tagInfo.Type)
				continue
			}
		}
	}

OUTPUT:
	return &protobuf.MenusResp{
		MatchData:    matchs,
		LiveData:     lives,
		CategoryData: summaryCategory,
	}, nil
}
